package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("ElderlyDisableLevelRel")
public class ElderlyDisableLevelRelController extends BaseController{


    @Autowired
    @Qualifier("ElderlyDisableLevelRelServiceImplOne")
    private ElderlyDisableLevelRelService elderlyDisableLevelRelService;

    @Autowired
    @Qualifier("DisableLevelServiceImplOne")
    private DisableLevelService disableLevelService;

    @Autowired
    @Qualifier("ElderlyServiceImplOne")
    private ElderlyService elderlyService;


    //编辑
    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String elderlyCode = jsonObject.getString("elderlyCode");
            JSONArray listStaff = jsonObject.getJSONArray("listDisableLevel");


            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/CommendCenterStaffRel/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            //删除老的关系
            List<ElderlyDisableLevelRel> listStaffRel = elderlyDisableLevelRelService.ListByElderlyCode(elderlyCode);
            for(ElderlyDisableLevelRel commandCenterStaffRel : listStaffRel){
                elderlyDisableLevelRelService.delete(commandCenterStaffRel.getElderlyDisableLevelRelCode());
            }

            //增加新的关系
            for (int k = 0; k < listStaff.size(); k++) {

                String diseaseTypeCode = listStaff.getString(k);

                ElderlyDisableLevelRel commandCenterStaffRel = new ElderlyDisableLevelRel();
                commandCenterStaffRel.setElderlyDisableLevelRelCode(UUID.randomUUID().toString());
                commandCenterStaffRel.setElderlyCode(elderlyCode);
                commandCenterStaffRel.setDisableLevelCode(diseaseTypeCode);
                elderlyDisableLevelRelService.insert(commandCenterStaffRel);
            }


            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/CommendCenterStaffRel/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //添加老人的病种关系
    @RequestMapping(value = "AddForOut",method = RequestMethod.POST)
    public MyResult AddForOut(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            if (!checkSignForOut(timeStr, sign)) {
                return failed("签名错误");
            }

            String elderlyCode = jsonObject.getString("elderlyCode");
            JSONArray listStaff = jsonObject.getJSONArray("listDisableLevel");

            Elderly elderly = elderlyService.getByElderlyCode(elderlyCode);

            //删除老的关系
            List<ElderlyDisableLevelRel> listStaffRel = elderlyDisableLevelRelService.ListByElderlyCode(elderlyCode);
            for(ElderlyDisableLevelRel commandCenterStaffRel : listStaffRel){
                elderlyDisableLevelRelService.delete(commandCenterStaffRel.getElderlyDisableLevelRelCode());
            }


            //增加新的关系
            for (int k = 0; k < listStaff.size(); k++) {

                String disableLevelName = listStaff.getString(k).trim();
                DisableLevel disableLevel = disableLevelService.getByDisableLevelName(disableLevelName);
                if(disableLevel == null){
                    disableLevel = new DisableLevel();
                    disableLevel.setDisableLevelCode(UUID.randomUUID().toString());
                    disableLevel.setName(disableLevelName);
                    disableLevel.setGovernmentCode(elderly.getGovernmentCode());
                    disableLevel.setInstitutionCode(elderly.getInstitutionCode());
                    disableLevelService.insert(disableLevel);
                }

                String disableLevelCode = disableLevel.getDisableLevelCode();

                ElderlyDisableLevelRel elderlyDisableLevelRel = new ElderlyDisableLevelRel();
                elderlyDisableLevelRel.setElderlyDisableLevelRelCode(UUID.randomUUID().toString());
                elderlyDisableLevelRel.setElderlyCode(elderlyCode);
                elderlyDisableLevelRel.setDisableLevelCode(disableLevelCode);
                elderlyDisableLevelRelService.insert(elderlyDisableLevelRel);
            }

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/CommendCenterStaffRel/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

}
